package com.serena.da.rest.services.deploy;

import com.serena.da.rest.common.Utils;
import com.serena.da.rest.http_helpers.Response;

import java.util.HashMap;

/**
 * Environment Service
 *
 * @author Spitkovskyi Igor
 */
public class EnvironmentService extends SraDeployService {
    private static EnvironmentService environmentService;

    private final String METHOD_ENVIRONMENT = "environment/";

    private EnvironmentService() throws Exception {
        super();
    }

    public static EnvironmentService getInstance() throws Exception {
        if (environmentService == null) {
            environmentService = new EnvironmentService();
        }
        return environmentService;
    }

    public String getRestUrlEnvironment() {
        return getRestUrl(METHOD_ENVIRONMENT);
    }

    /**
     * Add environment to application
     */
    public Response addEnvironmentToApplication(String applicationId, String environmentId, String requireApprovals, String lockSnapshots, String color, String inheritSystemCleanup) throws Exception {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("globalEnvironmentId", environmentId);
        hashMap.put("requireApprovals", requireApprovals);
        hashMap.put("lockSnapshots", lockSnapshots);
        hashMap.put("color", color);
        hashMap.put("inheritSystemCleanup", inheritSystemCleanup);
        hashMap.put("applicationId", applicationId);

        String jsonString = Utils.replaceJsonTemplateKeys("addEnvironmentToApplication.js", hashMap);

        return executeRequest(getRestUrlEnvironment(), Method.PUT, jsonString, "Environment in Application");
    }
}

    /*
    //CREATE LOCAL (APPLICATION) ENVIRONMENT BY ADDING GLOBAL ENVIRONMENT TO APPLICATION

    public boolean addEnvironmentToApplication(String ApplicationName, String[] EnvironmentNames) throws Exception {
        for (int i = 0; i < EnvironmentNames.length; i++) {
            try {
                addEnvironmentToApplication(ApplicationName, EnvironmentNames[i]);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }
     */